package com.scwatch.ext.quartz.system;


import com.scwatch.core.SpringContextUtil;
import com.scwatch.ext.domain.ScheduleJob;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author dxm
 * @Description: 映射启动方法
 * @date 2017年9月19日11:00:40
 */
public class TaskUtils {
    public final static Logger log = Logger.getLogger(TaskUtils.class);



    /**
     * 通过反射调用scheduleJob中定义的方法
     *
     * @param scheduleJob
     */
    public static void invokMethod(ScheduleJob scheduleJob) {
       Object object = null;
       Class clazz = null;
        if (StringUtils.isNotBlank(scheduleJob.getSpringId())) {
            if(SpringContextUtil.containsBean(scheduleJob.getSpringId())){
                object = SpringContextUtil.getBean(scheduleJob.getSpringId());
            }else{
                try {
                    clazz = Class.forName(scheduleJob.getBeanClass());
                    object = clazz.newInstance();
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
           /*---------------[ 注入 bean 暂停 ]-------------*/

        } else if (StringUtils.isNotBlank(scheduleJob.getBeanClass())) {
            try {
                clazz = Class.forName(scheduleJob.getBeanClass());
                object = clazz.newInstance();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
        if (object == null) {
            System.out.println("任务名称 = [" + scheduleJob.getJobName()+"."+scheduleJob.getMethodName() + "]---------------未启动成功，请检查是否配置正确！！！");
            return;
        }
        clazz = object.getClass();
        Method method = null;
        try {
            method = clazz.getDeclaredMethod(scheduleJob.getMethodName());

        } catch (NoSuchMethodException e) {
            try {
                method = clazz.getDeclaredMethod(scheduleJob.getMethodName(),String.class);

            }catch (NoSuchMethodException e1) {
                try {
                    method = clazz.getDeclaredMethod(scheduleJob.getMethodName(),Integer.class);

                }catch (NoSuchMethodException e2) {
                    System.out.println("任务名称 = [" + scheduleJob.getJobName()+"."+scheduleJob.getMethodName() + "]---------------未启动成功，方法名设置错误！！！");
                }
            }

        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if (method != null) {
            Object ValueObj = null;
            String value = scheduleJob.getParSet();
            if (StringUtils.isNotBlank(value) && !"null".equals(value)) {//查看是否有参数
                if (value.length()>0) {
                    if(!method.isAccessible()){
                        method.setAccessible(true);
                    }
                    //得到方法中的所有参数信息
                    Class<?>[] parameterClazz = method.getParameterTypes();
                    ValueObj =  fillList(parameterClazz[0], value);
                }
            }
            try {
                if(ValueObj==null) {
                    method.invoke(object);
                }else{
                    method.invoke(object, ValueObj);
                }
            } catch (IllegalAccessException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println("任务名称 = [" + scheduleJob.getJobName()+"."+scheduleJob.getMethodName() + "]----------启动成功");
        }else{
            System.out.println("任务名称 = [" + scheduleJob.getJobName()+"."+scheduleJob.getMethodName() + "]----------失败!");
        }
    }


    private static Object fillList( Class<?> parameter, Object value) {
        if ("java.lang.String".equals(parameter.getTypeName())) {
            return value;
        } else if ("java.lang.Character".equals(parameter.getTypeName())) {
            char[] ch = ((String) value).toCharArray();
            return ch[0];
        } else if ("char".equals(parameter.getTypeName())) {
            char[] ch = ((String) value).toCharArray();
            return ch[0];
        } else if ("java.lang.Double".equals(parameter.getTypeName())) {
            return Double.parseDouble((String) value);
        } else if ("double".equals(parameter.getTypeName())) {
            return Double.parseDouble((String) value);
        } else if ("java.lang.Integer".equals(parameter.getTypeName())) {
            return Integer.parseInt((String) value);
        } else if ("int".equals(parameter.getTypeName())) {
            return Integer.parseInt((String) value);
        } else if ("java.lang.Long".equals(parameter.getTypeName())) {
            return Long.parseLong((String) value);
        } else if ("long".equals(parameter.getTypeName())) {
            return Long.parseLong((String) value);
        } else if ("java.lang.Float".equals(parameter.getTypeName())) {
            return Float.parseFloat((String) value);
        } else if ("float".equals(parameter.getTypeName())) {
            return Float.parseFloat((String) value);
        } else if ("java.lang.Short".equals(parameter.getTypeName())) {
            return Short.parseShort((String) value);
        } else if ("shrot".equals(parameter.getTypeName())) {
            return Short.parseShort((String) value);
        } else if ("java.lang.Byte".equals(parameter.getTypeName())) {
            return Byte.parseByte((String) value);
        } else if ("byte".equals(parameter.getTypeName())) {
            return Byte.parseByte((String) value);
        } else if ("java.lang.Boolean".equals(parameter.getTypeName())) {
            if ("false".equals(value) || "0".equals(value)) {
                return false;
            } else if ("true".equals(value) || "1".equals(value)) {
                return true;
            }
        } else if ("boolean".equals(parameter.getTypeName())) {
            if ("false".equals(value) || "0".equals(value)) {
                return false;
            } else if ("true".equals(value) || "1".equals(value)) {
                return true;
            }
        }
        return null;
    }

}
